package com.downhometech.productcalculator.activities;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import com.downhometech.productcalculator.controllers.ProductController;
import com.downhometech.productcalculator.models.Cart;
import com.downhometech.productcalculator.models.Product;

public class EditProductActivity  extends DefaultActivity
{
	private EditText fieldName = null;
	private EditText fieldDescription = null;
	private EditText fieldNumber = null;
	private EditText fieldPrice = null;
	
	private RadioButton fieldIsCategoryYes = null;
	private RadioButton fieldIsCategoryNo = null;
	private RadioGroup fieldIsCategory = null;
	
	private Spinner fieldCategoryId = null;
	
	private TextView textListedPrice = null;
	private Product product = null;
	private Cart cart = null;
	private ProductController controller = null;
	
	
	public void onCreate(Bundle savedInstanceState) 
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.edit_product);
	    
	    controller = new ProductController(this);

		Bundle passedInBundle = this.getIntent().getExtras();
		int productId = 0;
		if (passedInBundle != null)
		{
			productId = passedInBundle.getInt("_productId");
		}
		//we're adding a new product here
		if (productId == 0) { product = new Product(); }
		else { product = controller.getProductById(productId); }
		
	    fieldName = (EditText) findViewById(R.id.ProductName);
	    fieldDescription = (EditText) findViewById(R.id.ProductDescription);
	    fieldPrice = (EditText) findViewById(R.id.ProductCustomPrice);
	    fieldNumber = (EditText) findViewById(R.id.ProductNumber);
	    textListedPrice = (TextView) findViewById(R.id.ProductListedPrice);

		fieldIsCategory = (RadioGroup) findViewById(R.id.ProductIsCategory);
		fieldIsCategoryYes = (RadioButton) findViewById(R.id.ProductIsCategoryYes);
		fieldIsCategoryNo = (RadioButton) findViewById(R.id.ProductIsCategoryNo);
		fieldCategoryId = (Spinner) findViewById(R.id.ProductCategoryId);
		
	    fieldName.setText(product.getName());
	    fieldDescription.setText(product.getDescription());
	    fieldPrice.setText(ProductController.formatDouble(product.getPrice()));
	    fieldNumber.setText(product.getNumber());
	    textListedPrice.setText("    Pre-Loaded Price: " + ProductController.formatCurrency(product.getListedPrice()));
	    
	    ArrayList<Product> categories = controller.getAllCategories();
	    categories.add(0, new Product(0, "Top Level", ""));
	    ArrayAdapter adapter = new ArrayAdapter(this, R.layout.productlist_item, categories);
        fieldCategoryId.setAdapter(adapter);
        
	    if (product.isCategory()) { fieldIsCategoryYes.setChecked(true); }
	    else  { fieldIsCategoryNo.setChecked(true); }
	    
	    fieldCategoryId.setSelection(0);
	    for (int x=0; x<categories.size(); x++) {
	    	if (categories.get(x).getId() == product.getCategoryId())
	    		 fieldCategoryId.setSelection(x);
	    }
	   

        
        
	    Button saveButton = (Button) findViewById(R.id.ButtonSave);
	    saveButton.setOnClickListener(new View.OnClickListener() 
	    {
	 	   public void onClick(View view) 
	 	   {
	 		   Product selectedCategory = (Product)fieldCategoryId.getItemAtPosition(fieldCategoryId.getSelectedItemPosition());
	 		   
	 		   product.setCategoryId(selectedCategory.getId());
	 		   product.setName(fieldName.getText().toString());
	 		  //product.setDescription(fieldDescription.getText().toString());
	 		   product.setNumber(fieldNumber.getText().toString());
	 		   product.setUserConfigured(true);
	 		   
	 		   if (product.getId() > 0)
	 		   {
		 		   product.setCustomPrice(Double.parseDouble(fieldPrice.getText().toString()));
	 		   }
	 		   else
	 		   {
	 			   product.setListedPrice(Double.parseDouble(fieldPrice.getText().toString()));
	 		   }
	 		   controller.saveProduct(product, product.getId());
	 		   Intent intent = new Intent(EditProductActivity.this, CartActivity.class);
	 		   startActivity(intent);				
	 		   finish();
	 	   }
	    });
	    
	    Button cancelButton = (Button) findViewById(R.id.ButtonCancel);
	    cancelButton.setOnClickListener(new View.OnClickListener() {
	 	   public void onClick(View view) {
	 		   Intent intent = new Intent(EditProductActivity.this, CartActivity.class);
	 		   startActivity(intent);				
	 		   finish();
	 	   }
	    });	    
	    
	    Button deleteButton = (Button) findViewById(R.id.ButtonDelete);
	    if (product.getId() == 0) { ((LinearLayout)deleteButton.getParent()).removeView(deleteButton); }
	    
	    deleteButton.setOnClickListener(new View.OnClickListener() 
	    {
			public void onClick(View view) 
			{
				AlertDialog.Builder dialog = new AlertDialog.Builder(EditProductActivity.this);
				dialog.setMessage("Are you sure?");
				dialog.setTitle("Press \"OK\" to delete this product.");
				dialog.setPositiveButton("OK", dialogClickListener);
				dialog.setNegativeButton("Cancel", dialogClickListener);
				dialog.show();
			}
			private DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() 
			{
			    @Override
			    public void onClick(DialogInterface dialog, int which)
			    {
			        switch (which){
			        case DialogInterface.BUTTON_POSITIVE:
			        	controller.deleteProduct(product);
			        	finish();
			            break;
			
			        case DialogInterface.BUTTON_NEGATIVE:
			            break;
			        }
			    }
			};		 	   
	    });	   	    
	}
	




}